﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FeatureModelCore;
using System.Windows;
using System.Windows.Media;
using FeatureModelCore.FMTypes;
using System.Windows.Shapes;
using FeatureModelCore.FMShapes;

namespace FeatureModelTool.FeatureModelGraphical.GraphicalControls
{
    public class Control_Relationship : IControl
    {
        public readonly static int DEFAULT_RADIUS = 2;

        public FeatureModelGraphicalView Canvas { get { return _canvas; } }
        public bool IsFocus { get { return _isFocus; } }
        public Brush Background
        {
            get
            {
                if (_groupRelationship.Type == FMTreeGroupRelationshipType.GroupOptional)
                {
                    return Brushes.White;
                }
                return Brushes.Black;
            }
        }

        public int BorderThickness { get { return FeatureModelGraphicalView.DEFAULT_THICKNESS; } }

        #region Fields
        private FeatureModelGraphicalView _canvas;
        private FeatureTreeGroupRelationship _groupRelationship;
        private FeatureTreeNode _node;
        private bool _isFocus;

        private Path _line;
        private Path _endBigPoint;
        #endregion

        public Control_Relationship(FeatureModelGraphicalView canvas
            , FeatureTreeGroupRelationship groupRelationship
            , FeatureTreeNode node)
        {
            _canvas = canvas;
            _groupRelationship = groupRelationship;
            _node = node;
            _isFocus = false;

            _line = new Path();
            _endBigPoint = new Path();
            RefreshPosition();
        }

        public UIElement[] ToUIElement()
        {
            if (_groupRelationship.Type == FMTreeGroupRelationshipType.Alternative
                || _groupRelationship.Type == FMTreeGroupRelationshipType.Or)
            {
                return new UIElement[] { _line };
            }
            return new UIElement[] { _line, _endBigPoint };
        }

        #region Focus
        /// <summary>
        /// Set Focus
        /// </summary>
        public void Focus()
        {
            this._canvas.ClearFocus();
            this._isFocus = true;
        }
        /// <summary>
        /// Clear Focus
        /// </summary>
        public void ClearFocus()
        {
            _isFocus = false;
        }
        #endregion

        public void SetPosition(FMPoint point, bool toRefresh)
        {
            throw new NotImplementedException();
        }

        public void SetPosition(double x, double y, bool toRefresh)
        {
            throw new NotImplementedException();
        }

        public void RefreshPosition()
        {
            FMCircle parentShape = _groupRelationship.Shape;
            FMRectangle childShape = _node.Shape;

            #region Line
            LineGeometry line = new LineGeometry();

            double startPosX = parentShape.Center.X;
            double startPoxY = parentShape.Center.Y
                + parentShape.Radius;

            //startPoxY = parentShape.Center.Y
            //    - parentShape.Radius;

            double endPosX = childShape.Center.X;
            double endPosY = childShape.Center.Y - childShape.Height / 2 - DEFAULT_RADIUS;
            if (_groupRelationship.Type == FMTreeGroupRelationshipType.Alternative
                || _groupRelationship.Type == FMTreeGroupRelationshipType.Or)
            {
                endPosY = childShape.Center.Y - childShape.Height / 2;
            }

            line.StartPoint = new Point(startPosX, startPoxY);
            line.EndPoint = new Point(endPosX, endPosY);

            _line.Data = line;

            _line.Stroke = Brushes.Black;
            _line.StrokeThickness = BorderThickness;
            #endregion

            #region End Point
            double x = childShape.Center.X;
            double y = childShape.Center.Y - childShape.Height / 2 - DEFAULT_RADIUS;

            EllipseGeometry ellipse = new EllipseGeometry(new Point(x, y)
                , DEFAULT_RADIUS, DEFAULT_RADIUS);


            _endBigPoint.Data = ellipse;

            _endBigPoint.StrokeThickness = BorderThickness;
            _endBigPoint.Fill = Background;
            _endBigPoint.Stroke = Brushes.Black;
            #endregion
        }
    }
}
